package edu.gatech.oad.ui;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import edu.gatech.oad.doctorsorder.SearchPanel;
import edu.gatech.oad.records.PatientHistory;
import edu.gatech.oad.records.PatientHistoryPanel;
import edu.gatech.oad.records.TreatmentRecordPanel;
import edu.gatech.oad.schedule.AppointmentPanel;
import edu.gatech.oad.schedule.CheckedInPanel;
import edu.gatech.oad.user.AccountsController;
import edu.gatech.oad.user.Doctor;
import edu.gatech.oad.user.MedicalPersonnel;
import edu.gatech.oad.user.Patient;
import edu.gatech.oad.user.Receptionist;
import edu.gatech.oad.user.SystemAdministrator;
import edu.gatech.oad.user.User;
import edu.gatech.oad.user.UserEditPanel;
import edu.gatech.oad.user.UserProfilePanel;

public class UserPanel extends JPanel{
        private static final long serialVersionUID = 1L;
        private JTabbedPane tabbedPane = new JTabbedPane();
        private int start = 1;
        private CheckedInPanel checkInPanel;
        protected User user;
        public UserPanel(User user){
                this.user = user;
                this.setBackground(Color.white);
                this.setLayout(new BorderLayout());
                this.setAlignmentX(Component.LEFT_ALIGNMENT);
                tabbedPane.setTabPlacement(JTabbedPane.LEFT);
                tabbedPane.addChangeListener(new UpdatePanelListener());
                this.checkInPanel = new CheckedInPanel();
                this.add(checkInPanel, BorderLayout.EAST);
                this.checkInPanel.setVisible(false);
                addPanels();
        }
        
        public void addPanels(){
                if(user != null){
                    tabbedPane.addTab("Home", new UserProfilePanel(user, this, user));                      
                    if(user instanceof  Patient){
                    	checkInPanel.setVisible(false);
                    	System.out.println(checkInPanel.isVisible());
                    }else{
                    	List<User> listOfUsers = new ArrayList<User>();
                    	for(User users : AccountsController.sharedController().getUsers()){
                    		listOfUsers.add(users);
                    	}
                    	tabbedPane.addTab("Search", null, new SearchPanel(listOfUsers, this, user)); 
                    	checkInPanel.setVisible(true);
                    }
                		add(tabbedPane, BorderLayout.CENTER);
                		start = tabbedPane.getTabCount();
                }
        }
        
        public void makeExitable(int start, int count){
        	for(int i = start; i < count; i++){
        		tabbedPane.setTabComponentAt(i, new ButtonTabComponent(tabbedPane));
        	}
        }
        
        public void addNewTab(String title, JPanel panel){
                tabbedPane.add(title, panel);
                final int tabCount = tabbedPane.getTabCount();
                makeExitable(start, tabCount);
                tabbedPane.setSelectedIndex(tabCount-1);
        }
        
        public void setUserForPanel(User newUser){
                this.user = newUser;
                this.updateUI();
                addPanels();
        }
        
        public void removeAllTabs(){
                tabbedPane.removeAll();
        }
        
        public JTabbedPane getTabbedPane(){
                return tabbedPane;
        }
        
        public User getUser(){
                return user;
        }

        public void addNewTab(String title, Component add) {
                tabbedPane.add(title, add);
                final int tabCount = tabbedPane.getTabCount();
                makeExitable(start, tabCount);
                tabbedPane.setSelectedIndex(tabCount-1);     
                this.updateUI();
        }
        
        private class UpdatePanelListener implements ChangeListener{
    		public void stateChanged(ChangeEvent e){
    			JTabbedPane tabSource = (JTabbedPane) e.getSource();
    			Component curComp = tabSource.getSelectedComponent();
    			if(curComp instanceof PatientHistoryPanel){
    				((PatientHistoryPanel) curComp).updatePanel();
    			}
    			else if(curComp instanceof TreatmentRecordPanel)
    				((TreatmentRecordPanel) curComp).updatePanel();
    		}
    	}
}
